package com.github.murzagalin.evaluator.function

abstract class Function(val name: String, val argsCount: IntRange) {

    constructor(name: String, argsCount: Int): this(name, argsCount..argsCount)

    constructor(name: String, minArgsCount: Int, maxArgsCount: Int): this(name, minArgsCount..maxArgsCount)

    abstract operator fun invoke(vararg args: Any): Any

    companion object
}

fun <T> Array<T>.getAsNumber(index: Int, lazyMessage: () -> Any): Number {
    val e = get(index)
    require(e is Number, lazyMessage)

    return e
}

private fun <T> Array<T>.getAsInt(index: Int, lazyMessage: () -> Any): Int {
    val e = get(index)
    require(e is Int, lazyMessage)

    return e
}

private fun <T> Array<T>.getAsDouble(index: Int, lazyMessage: () -> Any): Double {
    val e = get(index)
    require(e is Number, lazyMessage)

    return e.toDouble()
}

private fun <T> Array<T>.getAsBoolean(index: Int, lazyMessage: () -> Any): Boolean {
    val e = get(index)
    require(e is Boolean, lazyMessage)

    return e
}

